package com.xtgg.main;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;
import com.umeng.update.UmengUpdateAgent;
import com.xtgg.R;
import com.xtgg.baby.BabyListActivity;
import com.xtgg.history.HistorySumActivity;
import com.xtgg.more.MoreActivity;
import com.xtgg.sys.DemoApp;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity implements
		OnCheckedChangeListener {
	private long exitTime = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maintabs);
		
		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.update(this);
		UmengUpdateAgent.setDeltaUpdate(false);

		((RadioButton) findViewById(R.id.radio_button0))
				.setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.radio_button1))
				.setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.radio_button2))
				.setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.radio_button3))
				.setOnCheckedChangeListener(this);
		setupIntent();

		PushAgent mPushAgent = PushAgent.getInstance(this);
		mPushAgent.enable();
		// mPushAgent.setPushIntentServiceClass(MyPushIntentService.class);
		
		((RadioButton) findViewById(R.id.radio_button0)).setChecked(true);
		((DemoApp) getApplicationContext()).addActivity(this);
	}
	
	@Override
	protected void onDestroy() {
		((DemoApp) getApplicationContext()).removeActivity(this);
		super.onDestroy();
	}
	
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			switch (buttonView.getId()) {
			case R.id.radio_button0:
				this.getTabHost().setCurrentTabByTag("A_TAB");
				((RadioButton) findViewById(R.id.radio_button0)).setChecked(true);
				break;
			case R.id.radio_button1:
				this.getTabHost().setCurrentTabByTag("B_TAB");
				((RadioButton) findViewById(R.id.radio_button1)).setChecked(true);
				break;
			case R.id.radio_button2:
				this.getTabHost().setCurrentTabByTag("C_TAB");
				((RadioButton) findViewById(R.id.radio_button2)).setChecked(true);
				break;
			case R.id.radio_button3:
				this.getTabHost().setCurrentTabByTag("D_TAB");
				((RadioButton) findViewById(R.id.radio_button3)).setChecked(true);
				break;
			}
		}
	}

	private void setupIntent() {
		getTabHost().addTab(
				buildTabSpec("A_TAB", R.string.tab0, R.drawable.tab_0,
						new Intent(this, MainPageActivity.class)));
		getTabHost().addTab(
				buildTabSpec("B_TAB", R.string.tab1, R.drawable.tab_1,
						new Intent(this, BabyListActivity.class)));
		getTabHost().addTab(
				buildTabSpec("C_TAB", R.string.tab2, R.drawable.tab_2,
						new Intent(this, HistorySumActivity.class)));
		getTabHost().addTab(
				buildTabSpec("D_TAB", R.string.tab3, R.drawable.tab_3,
						new Intent(this, MoreActivity.class)));
	}

	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
			final Intent content) {
		return getTabHost()
				.newTabSpec(tag)
				.setIndicator(getString(resLabel),
						getResources().getDrawable(resIcon))
				.setContent(content);
	}

	@Override
	public void onBackPressed() {
		// Called by children
		exitApp();
	}

	public void exitApp() {
		if ((System.currentTimeMillis() - exitTime) > 2000) {
			Toast.makeText(this, "再按一次退出 动吧", Toast.LENGTH_LONG).show();
			exitTime = System.currentTimeMillis();
		} else {
			this.finish();
		}
	}
}
